package shupaiwen;
/*
 * 竖排V0.0
 * 提供竖排转横排的功能
 * 文件转换完成弹出提醒信息，两个选项，打开文件和取消
 * 相关推荐，进入igoogle主页，点击添加内容，搜索字符字或竖排版
 * 实现google竖排版的功能
 * 弄一首诗做预览
 * 感谢使用在离开时弹出（给用户选择，下次不再弹出，可以通过file.write记录状态，再通过file.read读入）
 * 增加初始化的功能，若不存在ini.txt，创建并写入初始状态，存在后就从中读取，并在程序运行过程中确实是否进行修改
 */

import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ShupaibanGUI {
	private Font font = new Font(Font.DIALOG_INPUT, 20, 15);
	private static int googlereminder=0;
	private static int count = 0;// 未使用软件就离开，给出提示，使用后一次后不提醒

	private JFrame win;

	private JTextArea textArea;

	public ShupaibanGUI() {
		init();
		show();
		System.setOut(new GUIPrintStream(System.out, textArea));
	}

	private void init() {
		win = new JFrame("竖排版工具");
		win.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		win.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				exit();
			}
		});
		JPanel content = creatContent();
		win.setContentPane(content);
	}

	protected void exit() {// 未使用软件就离开，给出提示，使用后一次后不提醒
		if (count != 0) {
			System.exit(0);
		} else {
			int i = JOptionPane.showConfirmDialog(null, "不试一试吗", "不试一试吗",
					JOptionPane.YES_NO_OPTION);
			if (i == 0)
				System.exit(0);
		}

	}

	private JPanel creatContent() {
		JPanel content = new JPanel(new BorderLayout());
		content.add(BorderLayout.CENTER, creatScrollPane());
		content.add(BorderLayout.EAST, creatFunctionPanel());
		return content;
	}

	private JScrollPane creatScrollPane() {
		JScrollPane scrollPane = new JScrollPane();
		textArea = new JTextArea();
		textArea.setFont(font);
		scrollPane.setViewportView(textArea);
		return scrollPane;
	}

	private JPanel creatFunctionPanel() {
		JPanel functionPanel = new JPanel(new GridLayout(3, 1));
		JButton preview = new JButton("预览");
		JButton transform = new JButton("转换文件");
		JButton exit = new JButton("离开");
		preview.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				count++;
				try {
					Function.preview();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

		transform.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				count++;
				String col = JOptionPane.showInputDialog("请输入竖排最大列数：");
				if(col!=null){
					while (true) {
						if (col.length() == 0) {
							col = JOptionPane.showInputDialog("输入有误，请重新输入");
							continue;
						}
						for (int i = 0; i < col.length(); i++) {
							if (col.charAt(i) < '0' || col.charAt(i) > '9')
								col = JOptionPane.showInputDialog("输入有误，请重新输入");
							continue;
						}
						break;
					}
					int cols = Integer.parseInt(col);
					int space = JOptionPane.showConfirmDialog(null, "列与列之间是否插入空格",
							"列与列之间是否插入空格", JOptionPane.YES_NO_OPTION);
					int spaces = (space + 1) % 2;
					try {
						Thread.sleep(500);
					} catch (InterruptedException e2) {
						e2.printStackTrace();
					}
					JFileChooser chooser = new JFileChooser();
					chooser.setMultiSelectionEnabled(true);
					int option = chooser.showOpenDialog(null);
					if (option == JFileChooser.APPROVE_OPTION) {
						File[] sf = chooser.getSelectedFiles();
						String st = sf[0].getPath();
						String code = codeDetect(st);
						String str = null;
						try {
							str = ReadinWriteout.readIn(st, cols, spaces, code);
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						ReadinWriteout.writeOut(st.substring(0, st.length() - 4)
								+ "-竖排.txt", str);
						System.out.println("文件保存在\n"+st.substring(0, st.length() - 4)
								+ "-竖排.txt");
						
						if(googlereminder==0){
							System.out.println("\n**************分隔符**************");
							System.out.println("google小工具，字符字");
							googlereminder++;
						}
					}
				}
			}

			private String codeDetect(String st) {
				CodepageDetectorProxy detector = CodepageDetectorProxy
						.getInstance();
				detector.add(JChardetFacade.getInstance());
				Charset charset = null;
				st = URLDecoder.decode(st);
				File f = new File(st);
				try {
					charset = detector.detectCodepage(f.toURL());
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				return charset.name();
			}
		});

		exit.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				exit();
			}
		});
		functionPanel.add(preview);
		functionPanel.add(transform);
		functionPanel.add(exit);
		return functionPanel;
	}

	private void show() {
		win.setVisible(true);
		win.setSize(450, 300);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ShupaibanGUI();
				System.out
						.println("使用说明\n预览功能：将预览本使用说明的竖排输出，\n    输出分为列与列之间有空隔和无空隔\n转换功能：需要你输入竖排的列数，\n    及确定是否在列与列之间插入空格，\n    转换后的文件会存在原文件的相同文件夹中，\n    并以 －竖排 标识\n其他：数字、字母会转为全角，以方便对齐\n\n");
			}
		});
	}

}
